package com.zy.openchat.core.model.user;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.core.model.message.ChatType;
import com.zy.openchat.core.model.photo.ProfilePhoto;

public class UserFullInfo implements Parcelable,AbsGroupInfo{
    public boolean is_blocked;
    public String share_text;
    public long id;
    public ChatType type;
    public String title;
    public ProfilePhoto photo;
    public UserInfo userInfo;
    public String bio;

    @Override
    public ProfilePhoto photo() {
        return photo;
    }

    @Override
    public String title() {
        return title;
    }

    @Override
    public long chatId() {
        return id;
    }




    public boolean isSuperGroup() {
        if (type != null) {
            return "chatTypeSupergroup".equals(type.type);
        }
        return false;
    }

    public boolean isSecretChat() {
        if (type != null) {
            return "chatTypeSecret".equals(type.type)||"chatTypePrivate".equals(type.type);
        }
        return false;
    }

    public boolean isMessageEqual(UserFullInfo fullInfo){
        if(fullInfo==null){
            return false;
        }
        return fullInfo.id == fullInfo.id;
    }

    public boolean isAvaterDownloaded = false;

    public boolean isAvaterDownloadEqual(UserFullInfo fullInfo){
        if(fullInfo==null){
            return false;
        }
        UserInfo oldUserInfo = UserInfo.getUserInfo(type.user_id);
        UserInfo newUserInfo = UserInfo.getUserInfo(fullInfo.type.user_id);
        if(oldUserInfo==null&&newUserInfo==null){
            return true;
        }
        if(oldUserInfo==null||newUserInfo==null){
            return false;
        }
        boolean newIsAvaterDownloaded = false;
        if(newUserInfo.profile_photo!=null&&!newUserInfo.profile_photo.isSmallPhotoDownloaded()){
            newIsAvaterDownloaded = true;
        }
        return newIsAvaterDownloaded == isAvaterDownloaded;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(this.is_blocked ? (byte) 1 : (byte) 0);
        dest.writeString(this.share_text);
        dest.writeLong(this.id);
        dest.writeParcelable(this.type, flags);
        dest.writeString(this.title);
        dest.writeParcelable(this.photo, flags);
        dest.writeParcelable(this.userInfo, flags);
        dest.writeString(this.bio);
    }

    public void readFromParcel(Parcel source) {
        this.is_blocked = source.readByte() != 0;
        this.share_text = source.readString();
        this.id = source.readLong();
        this.type = source.readParcelable(ChatType.class.getClassLoader());
        this.title = source.readString();
        this.photo = source.readParcelable(ProfilePhoto.class.getClassLoader());
        this.userInfo = source.readParcelable(UserInfo.class.getClassLoader());
        this.bio = source.readString();
    }

    public UserFullInfo() {
    }

    protected UserFullInfo(Parcel in) {
        this.is_blocked = in.readByte() != 0;
        this.share_text = in.readString();
        this.id = in.readLong();
        this.type = in.readParcelable(ChatType.class.getClassLoader());
        this.title = in.readString();
        this.photo = in.readParcelable(ProfilePhoto.class.getClassLoader());
        this.userInfo = in.readParcelable(UserInfo.class.getClassLoader());
        this.bio = in.readString();
    }

    public static final Creator<UserFullInfo> CREATOR = new Creator<UserFullInfo>() {
        @Override
        public UserFullInfo createFromParcel(Parcel source) {
            return new UserFullInfo(source);
        }

        @Override
        public UserFullInfo[] newArray(int size) {
            return new UserFullInfo[size];
        }
    };
}
